{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def gen(self, seq) -> [int]:\n",
    "        length = len(seq)\n",
    "        # 设置边界\n",
    "        seq = seq + '0'\n",
    "        n_seq = ''\n",
    "        left = 0\n",
    "        while left < length:\n",
    "            right = left\n",
    "            while right < length:\n",
    "                right = right + 1\n",
    "                if seq[left] == seq[right]:\n",
    "                    continue\n",
    "                else:\n",
    "                    num = right - left\n",
    "                    break\n",
    "            # 连续多个相同数字\n",
    "            if num > 1:\n",
    "                n_seq = n_seq + str(num)\n",
    "                n_seq = n_seq + seq[left]\n",
    "            else:\n",
    "                n_seq = n_seq + '1'\n",
    "                n_seq = n_seq + seq[left]\n",
    "            left = right\n",
    "        return n_seq\n",
    "    def countAndSay(self, n: int) -> str:\n",
    "        seq = '1'\n",
    "        for i in range(n-1):\n",
    "            seq = self.gen(seq)\n",
    "        return seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1211'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Solution()\n",
    "str(s.countAndSay(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.     1\n",
    "# 2.     11\n",
    "# 3.     21\n",
    "# 4.     1211\n",
    "# 5.     111221\n",
    "# 6.     312211\n",
    "# 7.     13112221"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3",
   "language": "python",
   "name": "py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
